package de.slothsoft.commons.swing;

import java.awt.Color;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class SlothsoftRenderer {

    private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    private static JCheckBox checkBox;

    public static JComponent formatRendererComponent(JLabel component, Object value) {

	component.setText(null);
	component.setIcon(null);

	if (value == null)
	    return component;

	if (value instanceof Boolean) {
	    getCheckBox().setSelected(((Boolean) value).booleanValue());
	    return checkBox;
	}

	component.setHorizontalAlignment(SwingConstants.LEFT);
	if (value instanceof Number) {
	    component.setHorizontalAlignment(SwingConstants.RIGHT);
	}

	if (value instanceof Icon) {
	    component.setIcon((Icon) value);
	} else if (value instanceof Date) {
	    component.setText(DATE_FORMAT.format((Date) value));
	} else {
	    component.setText(DisplayUtil.toString(value));
	}

	return component;
    }

    public static JCheckBox getCheckBox() {
	if (checkBox == null) {
	    checkBox = new JCheckBox();
	    // maybe more dynamic
	    checkBox.setBackground(Color.WHITE);
	}
	return checkBox;
    }

}
